package dao;

import model.Goods;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import utils.JDBCUtils;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class GoodsDao {
    public List<Map<String, Object>> getGoodsList(int recommendType) throws SQLException {
        QueryRunner r = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select g.id,g.name,g.cover,g.price,t.name typename from recommend r,goods g,type t where type=? and r.goods_id=g.id and g.type_id=t.id";
        return r.query(sql, new MapListHandler(), recommendType);
    }

    public List<Map<String, Object>> getScrollGood() throws SQLException {
        QueryRunner r = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select g.id,g.name,g.cover,g.price from recommend r,goods g where r.goods_id=g.id";
        return r.query(sql, new MapListHandler());
    }

    public Goods getGoodsById(int id) throws SQLException {
        QueryRunner r = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select g.id,g.name,g.cover,g.image1,g.image2,g.price,g.intro,g.stock,t.id typeid,t.name typename from goods g,type t where g.id = ? and g.type_id=t.id";
        return r.query(sql, new BeanHandler<>(Goods.class), id);
    }
}